package com.example.evaluate.service;

import com.example.evaluate.mapper.QuestionMapper;
import com.example.evaluate.mapper.TeacherMapper;
import com.example.evaluate.model.Question;
import com.example.evaluate.model.Teacher;
import com.example.evaluate.model.TrueTeacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TeacherService implements ITeacherService{

    @Autowired
    TeacherMapper teacherMapper;

    @Autowired
    QuestionMapper questionMapper;

    /**
     * 修改老师密码
     *
     * @param trueTeacher
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updatePassword(TrueTeacher trueTeacher) {

        int res = teacherMapper.updatePassword(trueTeacher);

        return res > 0;
    }

    /**
     * 老师登陆
     *
     * @param trueTeacher
     */
    @Override
    public boolean login(TrueTeacher trueTeacher) {

        TrueTeacher res = teacherMapper.findTrueTeacherByTeacherId(trueTeacher.getTrueTeacherId());
        if (res == null)
            return false;

        if (res.getTrueTeacherPassword().equals(trueTeacher.getTrueTeacherPassword()))
            return true;
        else
            return false;
    }

    /**
     * 修改老师部分信息(分数  是否被评价)
     *
     * @param teacher
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateTeacherInformation(Teacher teacher) {
        int res = teacherMapper.updateTeacherInformation(teacher);

        return res > 0;
    }

    /**
     * 通过老师id找问题
     *
     * @param teacherId
     */
    @Override
    public Question findQuestionByTeacherId(Integer teacherId) {
        return questionMapper.findQuestionByTeacherId(teacherId);
    }

    /**
     * 更新问题
     *
     * @param question
     */
    @Override
    public boolean updateQuestion(Question question) {
        return questionMapper.updateQuestion(question) > 0;
    }

    /**
     * 通过课程名称找问题
     *
     * @param courseName
     */
    @Override
    public List<Question> findQuestionByCourseName(String courseName) {
        return questionMapper.findQuestionByCourseName(courseName);
    }

    /**
     * 获取课程学生参加总人数
     *
     * @param courseName
     */
    @Override
    public int getTotalStudentNumberByCourseName(String courseName) {
        return teacherMapper.getTotalStudentNumberByCourseName(courseName);
    }

    /**
     * 获取课程学生已经评教总人数
     *
     * @param courseName
     */
    @Override
    public int getEvaluatedNumberByCourseName(String courseName) {
        return teacherMapper.getEvaluatedNumberByCourseName(courseName);
    }


}
